package com.nic.view.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nic.view.R;
import com.nic.view.model.CommonChooseBean;


/**
 * Created by Administrator on 2018/8/23.
 */

public class ChunkAdapter extends CommonAdapter<CommonChooseBean> {
    public final static String BLUE = "0";
    public final static String YELLOW = "1";
    public final static String RED = "2";
    public final static String GREEN = "3";
    public final static String GRAY = "4";
    private int selectedPosition = -1;

    public void setSelectedPosition(int selectedPosition) {
        this.selectedPosition = selectedPosition;
        notifyDataSetChanged();
    }

    @Override
    public View getBindView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chunk, null);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.text);
            holder.textViewName = (TextView) convertView.findViewById(R.id.text_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        CommonChooseBean chooseBean = getList().get(position);

        if (selectedPosition == position) {
            holder.textView.setTextColor(Color.WHITE);
            int colorId = getColorId(parent.getContext(), chooseBean.getCode());
            holder.textView.setBackgroundColor(colorId);
        } else {
            int colorId = getColorId(parent.getContext(), chooseBean.getCode());
            holder.textView.setTextColor(colorId);
            holder.textView.setBackgroundResource(R.drawable.bg_main_gray);
        }
        holder.textView.setText("" + chooseBean.getId());//数字
        holder.textViewName.setText("" + chooseBean.getName());//内容
        return convertView;
    }

    public static int getColorId(Context context, String type) {
        int colorId;
        if ("0".equals(type)) {
            colorId = ContextCompat.getColorStateList(context, R.color.colorBlueDark).getDefaultColor();
        } else if ("1".equals(type)) {
            colorId = ContextCompat.getColorStateList(context, R.color.color_yellow).getDefaultColor();
        } else if ("2".equals(type)) {
            colorId = ContextCompat.getColorStateList(context, R.color.colorRed).getDefaultColor();
        } else if ("3".equals(type)) {
            colorId = ContextCompat.getColorStateList(context, R.color.green).getDefaultColor();
        } else if ("4".equals(type)) {
            colorId = ContextCompat.getColorStateList(context, R.color.gray).getDefaultColor();
        } else {
            try {
                colorId = Color.parseColor(type);//type代表色值
            } catch (Exception e) {
                colorId = Color.parseColor("#d4d4d4");//type代表色值
            }
        }
        return colorId;
    }

    class ViewHolder {
        TextView textView;
        TextView textViewName;
    }

    /**
     * @param num
     * @param name
     * @param colorState
     * @return
     */
    public static CommonChooseBean chunk(String num, String name, String colorState) {
        CommonChooseBean c = new CommonChooseBean();
        c.setId(num);
        c.setName(name);
        c.setCode(colorState);
        return c;
    }


}
